<?php

/**
 * Room Controller for RoomBooking Component
 * 
 * @version 1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license		GNU/GPL
 */
// No direct access
defined('_JEXEC') or die('Restricted access');

class RoomBookingControllerRoom extends RoomBookingController {

    /**
     * constructor (registers additional tasks to methods)
     * @return void
     */
    function __construct() {
        parent::__construct();

        // Register Extra tasks
        $this->registerTask('add', 'edit');
        $this->registerTask('apply', 'save');
    }

    /**
     * Method to display the view
     * @return void
     */
    function display() {
        $modelBuildings = & $this->getModel('buildings');
        $modelTypes = & $this->getModel('types');
        $view = $this->getView('room', 'html');
        $view->setModel($modelBuildings);
        $view->setModel($modelTypes);
        parent::display();
    }

    /**
     * display the edit form
     * @return void
     */
    function edit() {
        JRequest::setVar('view', 'room');
        JRequest::setVar('layout', 'form');
        JRequest::setVar('hidemainmenu', 1);

        $this->display();
    }

    /**
     * save a record and redirect
     * @return void
     */
    function save() {
        $room = JRequest::get('POST');
        if ($room['name'] && $room['id_building'] && isset($room['open'])) {
            $model = $this->getModel('room');
            
            if ($returnid = $model->store($post)) {
                $msg = JText::_('ROOMSAVED');
                $type = 'message';
                
                if($room['id'] == 0)$com = '新しい会議室が追加できた（';
                else{
                    $com = '会議室が更新できた（古い情報：Room Name: '.$_SESSION['roomname'].', Floor: '.$_SESSION['floor'].', Num Seats: '.$_SESSION['seats'].', Cost: '.$_SESSION['cost'].' 新しい情報：';
                }
                $comment = $com.' Room Name: '.$room['name'].', Floor: '.$room['floor'].', Num Seats: '.$room['num_seats'].', Room Cost: '.$room['cost'].'）';
                $this->roomLog($comment);
            } else {
                $msg = JText::_('SAVEROOMERROR');
                $type = 'error';
            }
        } else {
            $msg = JText::_('FIELDMISSING');
            $type = 'error';
        }

        if ($this->getTask() == 'apply')
            $link = 'index.php?option=com_roombooking&controller=room&task=edit&cid=' . $returnid;
        else
            $link = 'index.php?option=com_roombooking&view=rooms';
        $this->setRedirect($link, $msg, $type);
    }

    function roomLog($comment) {
        jimport('joomla.error.log');
        // Define the file name as a PHP constant
        define('ROOM_LOG', 'room.log.' . date('Y_m_d') . '.php');

        $log = &JLog::getInstance(ROOM_LOG);

        // Just adding a comment
        $log->addEntry(array('comment' => $comment));
    }

    /**
     * remove record(s)
     * @return void
     */
    function remove() {
        $model = $this->getModel('room');
        
        if (!$model->delete()) {
            $msg = JText::_('NOROOMSDELETE');
            $type = 'error';
        } else {
            $msg = JText::_('ROOMSDELETE');
            $type = 'message';
        }

        $this->setRedirect('index.php?option=com_roombooking&view=rooms', $msg);
    }

    /**
     * cancel editing a record
     * @return void
     */
    function cancel() {
        $msg = JText::_('OPERCANC');
        $this->setRedirect('index.php?option=com_roombooking&view=rooms', $msg);
    }

}
